package com.tieland.demo.action;

import org.jfox.mvc.*;
import org.jfox.mvc.util.PagedList;
import org.jfox.mvc.annotation.ActionMethod;
import org.jfox.framework.annotation.Service;
import com.tieland.demo.bo.OrganBO;
import com.tieland.demo.entity.Organ;
import com.tieland.demo.common.util.ValueUtil;

import javax.ejb.EJB;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: Administrator
 * Date: 2008-3-12
 * Time: 8:37:26
 * To change this template use File | Settings | File Templates.
 */
@Service(id = "organ")
public class OrganAction extends ActionSupport {
    @EJB
    OrganBO organBO;

    @ActionMethod(name="all",successView = "ShowAllOrgan.vhtml",httpMethod = ActionMethod.HttpMethod.GET,invocationClass = OrganInvocation.class)
    public void doGetAll(InvocationContext invocationContext) throws Exception{
        OrganInvocation organInvocation = (OrganInvocation) invocationContext.getInvocation();

        PageContext pageContext = invocationContext.getPageContext();
        SessionContext sessionContext = invocationContext.getSessionContext();

        PagedList<Organ> organPagedList;

        List<Organ> organs = organBO.getOrganList();
        organPagedList = new PagedList<Organ>(organs,4);
        sessionContext.setAttribute("organPagedList",organPagedList);

        for(int i=0; i< organInvocation.getPage(); i++){
            organPagedList.nextPage();
        }

        int nextPage = organInvocation.getPage();
        if(!organPagedList.isLastPage()){
            nextPage++;
        }

        int previousPage = organInvocation.getPage();
        if(!organPagedList.isFirstPage()){
            previousPage--;
        }

        pageContext.setAttribute("pageList", organPagedList);
        pageContext.setAttribute("previousPage", previousPage);
        pageContext.setAttribute("currentPage",organInvocation.getPage());
        pageContext.setAttribute("nextPage", nextPage);
    }

    @ActionMethod(name="view",successView = "ShowOrgan.vhtml",httpMethod = ActionMethod.HttpMethod.GET,invocationClass = OrganInvocation.class)
    public void doGetView(InvocationContext invocationContext) throws Exception{
        OrganInvocation organInvocation = (OrganInvocation) invocationContext.getInvocation();

        PageContext pageContext = invocationContext.getPageContext();

        String organNo = organInvocation.getOrganNo();
        Organ organ = organBO.getOrgan(organNo);

        pageContext.setAttribute("organ", organ);
    }

    @ActionMethod(name="delete",successView = "ShowAllOrgan.vhtml",httpMethod = ActionMethod.HttpMethod.GET,invocationClass = OrganInvocation.class)
    public void doGetDelete(InvocationContext invocationContext) throws Exception{
        OrganInvocation organInvocation = (OrganInvocation) invocationContext.getInvocation();

        PageContext pageContext = invocationContext.getPageContext();
        SessionContext sessionContext = invocationContext.getSessionContext();

        String organNo = organInvocation.getOrganNo();
        Organ organ = organBO.getOrgan(organNo);
        if(organ != null ) organ.setFlag("0");
        organBO.updateOrgan(organ);

        PagedList<Organ> organPagedList;

        List<Organ> organs = organBO.getOrganList();
        organPagedList = new PagedList<Organ>(organs,4);
        sessionContext.setAttribute("organPagedList",organPagedList);

        for(int i=0; i< organInvocation.getPage(); i++){
            organPagedList.nextPage();
        }

        int nextPage = organInvocation.getPage();
        if(!organPagedList.isLastPage()){
            nextPage++;
        }

        int previousPage = organInvocation.getPage();
        if(!organPagedList.isFirstPage()){
            previousPage--;
        }

        pageContext.setAttribute("pageList", organPagedList);
        pageContext.setAttribute("previousPage", previousPage);
        pageContext.setAttribute("currentPage",organInvocation.getPage());
        pageContext.setAttribute("nextPage", nextPage);
    }

    @ActionMethod(name="update",successView = "ShowOrgan.vhtml",httpMethod = ActionMethod.HttpMethod.POST,invocationClass = OrganInvocation.class)
    public void doPostUpdate(InvocationContext invocationContext) throws Exception{
        OrganInvocation organInvocation = (OrganInvocation) invocationContext.getInvocation();

        PageContext pageContext = invocationContext.getPageContext();

        String organNo = organInvocation.getOrganNo();
        Organ organ = organBO.getOrgan(organNo);

        organ.setOrganLevel(organInvocation.getOrganLevel());
        organ.setOrganName(organInvocation.getOrganName());
        organ.setUpOrganNo(organInvocation.getUpOrganNo());
        if(ValueUtil.isNotNullOrEmpty(organInvocation.getFlag())){
            organ.setFlag(organInvocation.getFlag());
        }
        organBO.updateOrgan(organ);

        pageContext.setAttribute("organ", organ);
    }

    public static class OrganInvocation extends Invocation {
        private int page = 0;

        private String organNo;
        private String organName;
        private String organLevel;
        private String upOrganNo;
        private String flag;

        public int getPage() {
            return page;
        }

        public void setPage(int page) {
            this.page = page;
        }

        public String getOrganNo() {
            return organNo;
        }

        public void setOrganNo(String organNo) {
            this.organNo = organNo;
        }

        public String getOrganName() {
            return organName;
        }

        public void setOrganName(String organName) {
            this.organName = organName;
        }

        public String getOrganLevel() {
            return organLevel;
        }

        public void setOrganLevel(String organLevel) {
            this.organLevel = organLevel;
        }

        public String getUpOrganNo() {
            return upOrganNo;
        }

        public void setUpOrganNo(String upOrganNo) {
            this.upOrganNo = upOrganNo;
        }

        public String getFlag() {
            return flag;
        }

        public void setFlag(String flag) {
            this.flag = flag;
        }
    }
}
